Skip to content

Fix #14743 (New check: ftell() result is unspecified when file is opened in mode "t")#8360

Open
damorinCan wants to merge 4 commits into
cppcheck-opensource:mainfrom
damorinCan:CWE474_ftell
Open

Fix #14743 (New check: ftell() result is unspecified when file is opened in mode "t")#8360
damorinCan wants to merge 4 commits into
cppcheck-opensource:mainfrom
damorinCan:CWE474_ftell

Conversation

@damorinCan
Copy link
Copy Markdown

@damorinCan damorinCan commented Mar 21, 2026

@chrchr-github
Copy link
Copy Markdown
Collaborator

Thanks for your contribution.
As far as I can tell, this has nothing to do with Windows 11, but rather the C11 spec of ftell(), right? So the title of the PR should be adjusted.
Please add a test.

@damorinCan damorinCan changed the title ftell() have a different behavior in Windows 11 than before. ftell() not consistent in C11 Mar 21, 2026
@damorinCan
Copy link
Copy Markdown
Author

So the title of the PR should be adjusted.

Title now including reference to C11 and removed reference to Windows 11.

Please add a test.

I added a test in test/testio.cpp. Anything missing in it ?

@chrchr-github
Copy link
Copy Markdown
Collaborator

Please add a test.

I added a test in test/testio.cpp. Anything missing in it ?

Thanks, I must have missed that.

@chrchr-github
Copy link
Copy Markdown
Collaborator

Ping @danmar

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we need to have a ticket for this in trac

it feels like it should be added to the release notes as a "new check" ?

Comment thread lib/checkio.cpp Outdated
Comment thread test/testio.cpp Outdated
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Apr 13, 2026

I have tried to add some descriptions about some of our checkers here:
https://github.com/danmar/cppcheck/tree/main/man/checkers

Ideally that would contain descriptions for all our checkers but due to lack-of-resources it does not.

If you can add a corresponding file for this checking that would be great.

- Fix the uncrustify check (removed https reference to Microsoft).

New changes:
- Added missing string related to new check
- Added checker description for ftellTextModeFile
- Updated copyright.
@sonarqubecloud
Copy link
Copy Markdown

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Apr 24, 2026

We need a ticket in trac for this.

Can you please tell me what you think the summary and description should be and we can create it..

Comment thread man/checkers/ftellTextModeFile.md Outdated
Comment thread lib/checkio.h Outdated
Comment thread test/testio.cpp Outdated
Comment thread man/checkers/ftellTextModeFile.md Outdated
@damorinCan
Copy link
Copy Markdown
Author

We need a ticket in trac for this.

Can you please tell me what you think the summary and description should be and we can create it..

Thanks, I lost my login/password (last time was years ago). What about this:

[ftell() is no more returning the correct offset on a text file with Windows 11 compared to Linux.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented May 2, 2026

Thanks, I lost my login/password (last time was years ago). What about this:

do you remember your username? If you send me a new htpasswd hash via email I can update..

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented May 11, 2026

The PR title should be something like:

Fix #14743 (ftell() not consistent in C11)

I would like that the PR title and ticket title are consistent. and well imho the fundamental issue is not that linux/windows does not have the same behavior. the fundamental issue is that the behavior is implementation defined. it's possible you can have different behavior when using the same compiler but different volume type, compiler flags, etc..

@damorinCan damorinCan changed the title ftell() not consistent in C11 ftell() not consistent in between Linux/Unix and Windows when file is opened in mode "t" May 11, 2026
@damorinCan
Copy link
Copy Markdown
Author

damorinCan commented May 11, 2026 via email

@danmar danmar changed the title ftell() not consistent in between Linux/Unix and Windows when file is opened in mode "t" Fix #14743 (ftell() not consistent when file is opened in mode "t") May 12, 2026
Comment thread lib/checkio.cpp
void CheckIO::ftellFileError(const Token *tok)
{
reportError(tok, Severity::portability,
"ftellTextModeFile", "The ftell function obtains the current value of the file position indicator"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry but I want to change...

This is not a good warning message. How about:

ftell() result is unspecified when file is opened in mode "t"

I want that the standard text is quoted in the ftellTextModeFile.md file.

@danmar danmar changed the title Fix #14743 (ftell() not consistent when file is opened in mode "t") Fix #14743 (New check: ftell() result is unspecified when file is opened in mode "t") May 13, 2026
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented May 13, 2026

Could you add a note about this in the releasenotes? In my opinion we can say it's a new check. Technically it's not but it's a completely new type of warning.


position = fseek(f, 0, SEEK_END);
fstat(f, &st);
printf( "Position %d\n", ftell(f);
Copy link
Copy Markdown
Collaborator

@danmar danmar May 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to have a buggy code that shows why we should warn. There should be a mistake.

It seems clear to me that the developer of this code understands that the output of ftell() is a position and that it will not necessarily be the size.

printf( "Position %d\n", ftell(f);
printf( "File size %d\n, st.st_size);
fclose(f);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest such bad code:

Suggested change
}
// Wrong way to get file size:
FILE *f = fopen("Example.txt", "rt");
fseek(f, 0, SEEK_END);
printf("File size: %d\n", ftell(f));
fclose(f);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants